<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="指令代码" prop="zlmZldm">
        <el-input
          v-model="queryParams.zlmZldm"
          placeholder="请输入指令代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指令名称" prop="zlmZlmc">
        <el-input
          v-model="queryParams.zlmZlmc"
          placeholder="请输入指令名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:ZlmMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:ZlmMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:ZlmMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['base:ZlmMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      height="calc(100% - 100px)"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="ZlmMstrList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="指令代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmZldm"
      />
      <el-table-column
        label="指令名称"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmZlmc"
      />
      <el-table-column
        label="终端输入"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmCode"
      />
      <el-table-column
        label="看板显示"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmShow"
      />
      <el-table-column
        label="显示颜色"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmColor"
      />
      <el-table-column
        label="标准工时"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmBzgs"
      />
      <el-table-column
        label="屏蔽N秒"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmSecond"
      />
      <el-table-column
        label="字节数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmLen"
      />
      <el-table-column
        label="终端输入"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmSrflag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmSrflag" />
        </template>
      </el-table-column>
      <el-table-column
        label="穴数控制"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmXsflag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmXsflag" />
        </template>
      </el-table-column>
      <el-table-column
        label="计入不良"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmBlflag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmBlflag" />
        </template>
      </el-table-column>
      <el-table-column
        label="是否计数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmJsflag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmJsflag" />
        </template>
      </el-table-column>
      <el-table-column
        label="工单模式"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmGdflag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmGdflag" />
        </template>
      </el-table-column>
      <el-table-column
        label="有效标志"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmFlag"
      >
        <template #default="scope">
          <dict-tag :options="zlmFlagOptions" :value="scope.row.zlmFlag" />
        </template>
      </el-table-column>
      <el-table-column
        label="终端第一行显示信息"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmInfo1"
      />
      <el-table-column
        label="终端第二行显示信息"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmInfo2"
      />
      <el-table-column
        label="终端第三行显示信息"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmInfo3"
      />
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmDesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="zlmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.zlmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="zlmGgry"
      />
      <el-table-column
        label="更改日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="zlmGgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.zlmGgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:ZlmMstr:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:ZlmMstr:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改指令代码对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="指令代码" prop="zlmZldm">
            <el-input v-model="form.zlmZldm" placeholder="请输入指令代码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指令名称" prop="zlmZlmc">
            <el-input v-model="form.zlmZlmc" placeholder="请输入指令名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="终端输入" prop="zlmCode">
            <el-input v-model="form.zlmCode" placeholder="请输入终端输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="看板显示" prop="zlmShow">
            <el-input v-model="form.zlmShow" placeholder="请输入看板显示" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标准工时" prop="zlmBzgs">
            <el-input v-model="form.zlmBzgs" placeholder="请输入标准工时" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="屏蔽N秒" prop="zlmSecond">
            <el-input
              v-model="form.zlmSecond"
              placeholder="请输入屏蔽N秒內操作，用於統計次數"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示颜色" prop="zlmColor">
            <el-input v-model="form.zlmColor" placeholder="请输入显示颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字节数" prop="zlmLen">
            <el-input v-model="form.zlmLen" placeholder="请输入字节数" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="信息一" prop="zlmInfo1">
            <el-input
              v-model="form.zlmInfo1"
              placeholder="请输入终端第一行显示信息"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="信息二" prop="zlmInfo2">
            <el-input
              v-model="form.zlmInfo2"
              placeholder="请输入终端第二行显示信息"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="信息三" prop="zlmInfo3">
            <el-input
              v-model="form.zlmInfo3"
              placeholder="请输入终端第三行显示信息"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="终端输入">
            <el-radio-group v-model="form.zlmSrflag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="穴数控制">
            <el-radio-group v-model="form.zlmXsflag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="不良计数">
            <el-radio-group v-model="form.zlmBlflag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否计数">
            <el-radio-group v-model="form.zlmJsflag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工单模式">
            <el-radio-group v-model="form.zlmGdflag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效标识">
            <el-radio-group v-model="form.zlmFlag">
              <el-radio
                v-for="dict in zlmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注" prop="zlmDesc">
            <el-input v-model="form.zlmDesc" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listZlmMstr,
  getZlmMstr,
  delZlmMstr,
  addZlmMstr,
  updateZlmMstr,
  exportZlmMstr,
} from "@/api/base/ZlmMstr";

export default {
  name: "ZlmMstr",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指令代码表格数据
      ZlmMstrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      zlmFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        zlmZldm: null,
        zlmZlmc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zlmZldm: [
          { required: true, message: "指令代码不能为空", trigger: "blur" },
        ],
        zlmZlmc: [
          { required: true, message: "指令名称不能为空", trigger: "blur" },
        ],
        zlmCode: [
          { required: true, message: "终端输入不能为空", trigger: "blur" },
        ],
        zlmShow: [
          { required: true, message: "看板显示不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("base_zlm_flag").then((response) => {
      this.zlmFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询指令代码列表 */
    getList() {
      this.loading = true;
      this.queryParams.pageNum = 1;
      listZlmMstr(this.queryParams).then((response) => {
        this.ZlmMstrList = [];
        this.ZlmMstrList = this.ZlmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listZlmMstr(this.queryParams).then((response) => {
          this.ZlmMstrList = this.ZlmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zlmId: null,
        zlmCode: null,
        zlmZldm: null,
        zlmZlmc: null,
        zlmBzgs: null,
        zlmSecond: null,
        zlmShow: null,
        zlmColor: null,
        zlmLen: null,
        zlmInfo1: null,
        zlmInfo2: null,
        zlmInfo3: null,
        zlmSrflag: "0",
        zlmXsflag: "0",
        zlmJsflag: "0",
        zlmGdflag: "0",
        zlmBlflag: "0",
        zlmFlag: "0",
        zlmDesc: null,
        zlmJlry: null,
        zlmJlrq: null,
        zlmGgry: null,
        zlmGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.ZlmMstrList = [];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.zlmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加指令代码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const zlmId = row.zlmId || this.ids;
      getZlmMstr(zlmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指令代码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.zlmId != null) {
            updateZlmMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZlmMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const zlmIds = row.zlmId || this.ids;
      this.$confirm(
        '是否确认删除指令代码编号为"' + zlmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delZlmMstr(zlmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有指令代码数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportZlmMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
